#include "../Delta-Ray/console.h"

Console::Console(){
    //konstruktor
    for(int i=0; i<64; i++)key[i]=false;
}

void Console::Initc(SDL_Surface *a_S_SCREEN,TTF_Font *a_font,SDL_Surface *a_Cbg){
    S_SCREEN = a_S_SCREEN;
    Font_Arial = a_font;
    S_Cbg = a_Cbg;
    Color_Line = {255,255,255};
    Re_In = {2,225,0,0};
    Re_Cbg = {0,0,S_Cbg->w,S_Cbg->h};
    for(int i = 0; i<18; i++){
        Re_Line[i] = {2, i*12+0, 0, 0};
        console_out[i] = " ";
        S_Line[i] = TTF_RenderText_Solid(Font_Arial,console_out[i].c_str(),Color_Line);
    }
    console_in = "> ";
    S_Line_in = TTF_RenderText_Solid(Font_Arial,console_in.c_str(),Color_Line);
    NewMsg(ENG_VER);
    time(&dat);
    data2 = ctime(&dat);
    data = "Time: "+data2;
    NewMsg(data);
    NewMsg("Version: 0.01");
}

void Console::Tickc(Input *a_key){
    if(a_key->IsKeyDown() == true){
        if(a_key->GetLastKey() == KEY_q && key[1] == false){
            key[1] = true;
            console_in+="q";
        }
		if(a_key->GetLastKey() == KEY_w && key[2] == false){
            key[2] = true;
            console_in+="w";
        }
		if(a_key->GetLastKey() == KEY_e && key[3] == false){
            key[3] = true;
            console_in+="e";
        }
		if(a_key->GetLastKey() == KEY_r && key[4] == false){
            key[4] = true;
            console_in+="r";
        }
		if(a_key->GetLastKey() == KEY_t && key[5] == false){
            key[5] = true;
            console_in+="t";
        }
		if(a_key->GetLastKey() == KEY_y && key[6] == false){
            key[6] = true;
            console_in+="y";
        }
		if(a_key->GetLastKey() == KEY_u && key[7] == false){
            key[7] = true;
            console_in+="u";
        }
		if(a_key->GetLastKey() == KEY_i && key[8] == false){
            key[8] = true;
            console_in+="i";
        }
		if(a_key->GetLastKey() == KEY_o && key[9] == false){
            key[9] = true;
            console_in+="o";
        }
		if(a_key->GetLastKey() == KEY_p && key[10] == false){
            key[10] = true;
            console_in+="p";
        }
		if(a_key->GetLastKey() == KEY_a && key[11] == false){
            key[11] = true;
            console_in+="a";
        }
		if(a_key->GetLastKey() == KEY_s && key[12] == false){
            key[12] = true;
            console_in+="s";
        }
		if(a_key->GetLastKey() == KEY_d && key[13] == false){
            key[13] = true;
            console_in+="d";
        }
		if(a_key->GetLastKey() == KEY_f && key[14] == false){
            key[14] = true;
            console_in+="f";
        }
		if(a_key->GetLastKey() == KEY_g && key[15] == false){
            key[15] = true;
            console_in+="g";
        }
		if(a_key->GetLastKey() == KEY_h && key[16] == false){
            key[16] = true;
            console_in+="h";
        }
		if(a_key->GetLastKey() == KEY_j && key[17] == false){
            key[17] = true;
            console_in+="j";
        }
		if(a_key->GetLastKey() == KEY_k && key[18] == false){
            key[18] = true;
            console_in+="k";
        }
		if(a_key->GetLastKey() == KEY_l && key[19] == false){
            key[19] = true;
            console_in+="l";
        }
        if(a_key->GetLastKey() == KEY_z && key[20] == false){
            key[20] = true;
            console_in+="z";
        }
		if(a_key->GetLastKey() == KEY_x && key[21] == false){
            key[21] = true;
            console_in+="x";
        }
		if(a_key->GetLastKey() == KEY_c && key[22] == false){
            key[22] = true;
            console_in+="c";
        }
		if(a_key->GetLastKey() == KEY_v && key[23] == false){
            key[23] = true;
            console_in+="v";
        }
		if(a_key->GetLastKey() == KEY_b && key[24] == false){
            key[24] = true;
            console_in+="b";
        }
		if(a_key->GetLastKey() == KEY_n && key[25] == false){
            key[25] = true;
            console_in+="n";
        }
		if(a_key->GetLastKey() == KEY_m && key[26] == false){
            key[26] = true;
            console_in+="m";
        }
        if(a_key->GetLastKey() == KEY_SPACE && key[27] == false){
            key[27] = true;
            console_in+=" ";
        }
        if(a_key->GetLastKey() == KEY_m && key[26] == false){
            key[26] = true;
            console_in+="m";
        }
        if(a_key->GetLastKey() == KEY_m && key[26] == false){
            key[26] = true;
            console_in+="m";
        }
        if(a_key->GetLastKey() == KEY_m && key[26] == false){
            key[26] = true;
            console_in+="m";
        }
        if(a_key->GetLastKey() == KEY_m && key[26] == false){
            key[26] = true;
            console_in+="m";
        }
        if(a_key->GetLastKey() == KEY_m && key[26] == false){
            key[26] = true;
            console_in+="m";
        }
        if(a_key->GetLastKey() == KEY_m && key[26] == false){
            key[26] = true;
            console_in+="m";
        }
        if(a_key->GetLastKey() == KEY_m && key[26] == false){
            key[26] = true;
            console_in+="m";
        }
        if(a_key->GetLastKey() == KEY_m && key[26] == false){
            key[26] = true;
            console_in+="m";
        }
        if(a_key->GetLastKey() == KEY_m && key[26] == false){
            key[26] = true;
            console_in+="m";
        }
        if(a_key->GetLastKey() == KEY_SPACE && key[27] == false){
            key[27] = true;
            console_in+=" ";
        }
        if(a_key->GetLastKey() == KEY_1 && key[28] == false){
            key[28] = true;
            console_in+="1";
        }
		if(a_key->GetLastKey() == KEY_2 && key[29] == false){
            key[29] = true;
            console_in+="2";
        }
		if(a_key->GetLastKey() == KEY_3 && key[30] == false){
            key[30] = true;
            console_in+="3";
        }
		if(a_key->GetLastKey() == KEY_4 && key[31] == false){
            key[31] = true;
            console_in+="4";
        }
		if(a_key->GetLastKey() == KEY_5 && key[32] == false){
            key[32] = true;
            console_in+="5";
        }
		if(a_key->GetLastKey() == KEY_6 && key[33] == false){
            key[33] = true;
            console_in+="6";
        }
		if(a_key->GetLastKey() == KEY_7 && key[34] == false){
            key[34] = true;
            console_in+="7";
        }
		if(a_key->GetLastKey() == KEY_8 && key[35] == false){
            key[35] = true;
            console_in+="8";
        }
		if(a_key->GetLastKey() == KEY_9 && key[36] == false){
            key[36] = true;
            console_in+="9";
        }
		if(a_key->GetLastKey() == KEY_0 && key[37] == false){
            key[37] = true;
            console_in+="0";
        }
        if(a_key->GetLastKey() == KEY_BACKSPACE && key[38] == false){
            key[38] = true;
            console_in.erase(console_in.size()-1,1);
        }
		if(a_key->GetLastKey() == KEY_RETURN && key[39] == false){
		    key[39] = true;
		    console_in.erase(0,2);
		    SendCommand(console_in);
		    console_in = "> ";
        }
    }
    if(a_key->IsKeyUp() == true){
        		if(a_key->GetLastKey() == KEY_q)key[1] = false;
		if(a_key->GetLastKey() == KEY_w)key[2] = false;
		if(a_key->GetLastKey() == KEY_e)key[3] = false;
		if(a_key->GetLastKey() == KEY_r)key[4] = false;
		if(a_key->GetLastKey() == KEY_t)key[5] = false;
		if(a_key->GetLastKey() == KEY_y)key[6] = false;
		if(a_key->GetLastKey() == KEY_u)key[7] = false;
		if(a_key->GetLastKey() == KEY_i)key[8] = false;
		if(a_key->GetLastKey() == KEY_o)key[9] = false;
		if(a_key->GetLastKey() == KEY_p)key[10] = false;
		if(a_key->GetLastKey() == KEY_a)key[11] = false;
		if(a_key->GetLastKey() == KEY_s)key[12] = false;
		if(a_key->GetLastKey() == KEY_d)key[13] = false;
		if(a_key->GetLastKey() == KEY_f)key[14] = false;
		if(a_key->GetLastKey() == KEY_g)key[15] = false;
		if(a_key->GetLastKey() == KEY_h)key[16] = false;
		if(a_key->GetLastKey() == KEY_j)key[17] = false;
		if(a_key->GetLastKey() == KEY_k)key[18] = false;
		if(a_key->GetLastKey() == KEY_l)key[19] = false;
		if(a_key->GetLastKey() == KEY_z)key[20] = false;
		if(a_key->GetLastKey() == KEY_x)key[21] = false;
		if(a_key->GetLastKey() == KEY_c)key[22] = false;
		if(a_key->GetLastKey() == KEY_v)key[23] = false;
        if(a_key->GetLastKey() == KEY_b)key[24] = false;
		if(a_key->GetLastKey() == KEY_n)key[25] = false;
		if(a_key->GetLastKey() == KEY_m)key[26] = false;
		if(a_key->GetLastKey() == KEY_SPACE)key[27] = false;
		if(a_key->GetLastKey() == KEY_1)key[28] = false;
		if(a_key->GetLastKey() == KEY_2)key[29] = false;
		if(a_key->GetLastKey() == KEY_3)key[30] = false;
		if(a_key->GetLastKey() == KEY_4)key[31] = false;
		if(a_key->GetLastKey() == KEY_5)key[32] = false;
		if(a_key->GetLastKey() == KEY_6)key[33] = false;
		if(a_key->GetLastKey() == KEY_7)key[34] = false;
		if(a_key->GetLastKey() == KEY_8)key[35] = false;
		if(a_key->GetLastKey() == KEY_9)key[36] = false;
		if(a_key->GetLastKey() == KEY_0)key[37] = false;
		if(a_key->GetLastKey() == KEY_BACKSPACE)key[38] = false;
        if(a_key->GetLastKey() == KEY_RETURN)key[39] = false;
    }
}

void Console::Drawc(){
    S_Line_in = TTF_RenderText_Solid(Font_Arial,console_in.c_str(),Color_Line);
    SDL_BlitSurface(S_Cbg,NULL,S_SCREEN,&Re_Cbg);
    for(int i = 0; i<18; i++)S_Line[i] = TTF_RenderText_Solid(Font_Arial,console_out[i].c_str(),Color_Line);
    for(int i = 0; i<18; i++)SDL_BlitSurface(S_Line[i],NULL,S_SCREEN,&Re_Line[i]);
    SDL_BlitSurface(S_Line_in,NULL,S_SCREEN,&Re_In);
    SDL_FreeSurface(S_Line_in);
    for(int i = 0; i<18; i++)SDL_FreeSurface(S_Line[i]);
}

void Console::NewMsg(string a_msg){
    for(int i = 0; i<17; i++)console_out[i] = console_out [i+1];
    console_out[17] = a_msg;
}

bool Console::SendCommand(string a_cmd){
    NewMsg(a_cmd);
    if(a_cmd == "ping")NewMsg("PONG!");
    else if(a_cmd == "pong")NewMsg("PING!");
    else {
        string tmp = "Unkown command - ";
        tmp += a_cmd;
        NewMsg(tmp);
    }
}
